package com.telit.firecommand.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.telit.common.constant.Constant;
import com.telit.common.entity.DataReturn;
import com.telit.common.service.BaiduService;
import com.telit.common.util.BaiduUtils;
import com.telit.common.util.RedisUtils;
import com.telit.firecommand.constant.RedisKeyConstant;
import com.telit.firecommand.dto.GeoCunDTO;
import com.telit.firecommand.dto.SpreadDTO;
import com.telit.firecommand.entity.DailyWeather;
import com.telit.firecommand.entity.FireWeatherInfo;
import com.telit.firecommand.entity.NowWeather;
import com.telit.firecommand.service.FireWeatherInfoService;
import com.telit.firecommand.service.IGeoXzqhCunService;
import com.telit.firecommand.service.WeatherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2022/9/24 9:45
 */
@RestController
@RequestMapping(value = "/fire-weather")
@Api(tags = "火情天气处理控制器", value = "/fire-weather", produces = MediaType.APPLICATION_JSON_VALUE)
public class FireWeatherInfoController {

    @Autowired
    FireWeatherInfoService fireWeatherInfoService;

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private IGeoXzqhCunService geoXzqhCunService;

    @Autowired
    private BaiduService baiduService;

    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新增火情天气信息", httpMethod = Constant.REQUEST_METHOD_POST)
    public DataReturn add(@RequestBody FireWeatherInfo fireWeatherInfo) {
       // fireWeatherInfoService.add(fireWeatherInfo);
        return DataReturn.ok();
    }

    @GetMapping(value = "/weatherByAddress", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "weatherByAddress", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn weatherByAddress(@RequestParam String longitude,
                                       @RequestParam String latitude) {
        //经纬度查询所属 区县
        GeoCunDTO cunDTO = geoXzqhCunService.getDistrictByPoint(longitude, latitude);
        String rKey = null;
        if (cunDTO == null) {
            String resultStr = BaiduUtils.queryAddressByLaAndLo(longitude, latitude);
            JSONObject result = JSON.parseObject(resultStr, JSONObject.class);
//            String string = result.getJSONObject("result").getString("formatted_address");
            JSONObject addressComponent = JSON.parseObject(result.getJSONObject("result").getString("addressComponent"),
                    JSONObject.class);
            rKey = addressComponent.getString("city") + addressComponent.getString("district");
        } else {
            rKey = cunDTO.getXian();
        }

        //缓存过期时间 0.5小时
        Long expireTime = (long) (0.5 * 60 * 60);
        //获取当前实时天气
        NowWeather nowWeather;
        String nowWeatherJson = RedisUtils.get(RedisKeyConstant.NOW_WEATHER + rKey);
        if (StringUtils.isNotEmpty(nowWeatherJson)) {
            nowWeather = JSON.parseObject(nowWeatherJson, NowWeather.class);
        } else {
            nowWeather = weatherService.getNowWeather(latitude + ":" + longitude);
            RedisUtils.set(RedisKeyConstant.NOW_WEATHER + rKey, JSON.toJSONString(nowWeather), expireTime);
        }

        return DataReturn.ok(nowWeather);
    }

    @GetMapping(value = "/addFireWeatherInfoByPoint", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "通过点位新增火情天气信息", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn addFireWeatherInfoByPoint(@RequestParam String longitude,
                                                @RequestParam String latitude,
                                                @RequestParam String fireId) {
        //经纬度查询所属 区县
        GeoCunDTO cunDTO = geoXzqhCunService.getDistrictByPoint(longitude, latitude);
        String areaCode = cunDTO.getXian();

        //缓存过期时间 0.5小时
        Long expireTime = (long) (0.5 * 60 * 60);
        //获取当前实时天气
        NowWeather nowWeather;
        String nowWeatherJson = RedisUtils.get(RedisKeyConstant.NOW_WEATHER + areaCode);
        if (StringUtils.isNotEmpty(nowWeatherJson)) {
            nowWeather = JSON.parseObject(nowWeatherJson, NowWeather.class);
        } else {
            nowWeather = weatherService.getNowWeather(latitude + ":" + longitude);
            RedisUtils.set(RedisKeyConstant.NOW_WEATHER + areaCode, JSON.toJSONString(nowWeather), expireTime);
        }
        //获取当天天气
        DailyWeather dailyWeather;
        String dailyWeatherJson = RedisUtils.get(RedisKeyConstant.ONE_DAY_WEATHER + areaCode);
        if (StringUtils.isNotEmpty(dailyWeatherJson)) {
            dailyWeather = JSON.parseObject(dailyWeatherJson, DailyWeather.class);
        } else {
            dailyWeather = weatherService.getOneDayWeather(latitude + ":" + longitude, 0, 5);
            RedisUtils.set(RedisKeyConstant.ONE_DAY_WEATHER + areaCode, JSON.toJSONString(dailyWeather), expireTime);
        }

       // fireWeatherInfoService.addFireWeatherInfoByPoint(longitude, latitude, fireId, nowWeather, dailyWeather);

        return DataReturn.ok();
    }

//    @GetMapping(value = "/queryFireWeatherInfoNow", produces = "application/json;charset=UTF-8")
//    @ApiOperation(value = "查询火场实时气象", httpMethod = Constant.REQUEST_METHOD_GET)
//    public DataReturn<FireWeatherInfo> queryFireWeatherInfoNow(@RequestParam String longitude,
//                                                               @RequestParam String latitude) {
//        //经纬度查询所属 区县
//        GeoCunDTO cunDTO = geoXzqhCunService.getDistrictByPoint(longitude, latitude);
//        String areaCode = cunDTO.getXian();
//
//        //缓存过期时间 5小时
//        Long expireTime = (long) (5 * 60 * 60);
//        //获取当前实时天气
//        NowWeather nowWeather;
//        String nowWeatherJson = RedisUtils.get(RedisKeyConstant.NOW_WEATHER + areaCode);
//        if (StringUtils.isNotEmpty(nowWeatherJson)) {
//            nowWeather = JSON.parseObject(nowWeatherJson, NowWeather.class);
//        } else {
//            nowWeather = weatherService.getNowWeather(latitude + ":" + longitude);
//            RedisUtils.set(RedisKeyConstant.NOW_WEATHER + areaCode, JSON.toJSONString(nowWeather), expireTime);
//        }
//        //获取当天天气
//        DailyWeather dailyWeather;
//        String dailyWeatherJson = RedisUtils.get(RedisKeyConstant.ONE_DAY_WEATHER + areaCode);
//        if (StringUtils.isNotEmpty(dailyWeatherJson)) {
//            dailyWeather = JSON.parseObject(dailyWeatherJson, DailyWeather.class);
//        } else {
//            dailyWeather = weatherService.getOneDayWeather(latitude + ":" + longitude, 0, 5);
//            RedisUtils.set(RedisKeyConstant.ONE_DAY_WEATHER + areaCode, JSON.toJSONString(dailyWeather), expireTime);
//        }
//
//        FireWeatherInfo fireWeatherInfo = fireWeatherInfoService.queryFireWeatherInfoNow(latitude, longitude,
//                nowWeather, dailyWeather);
//
//        return DataReturn.ok(fireWeatherInfo);
//    }

    @GetMapping(value = "/getWeather", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询天气信息", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<List<DailyWeather>> getWeather(@RequestParam String longitude,
                                                     @RequestParam String latitude) {
        //经纬度查询所属 区县
        GeoCunDTO cunDTO = geoXzqhCunService.getDistrictByPoint(longitude, latitude);
        String rKey = null;
        if (cunDTO == null) {
            String resultStr = BaiduUtils.queryAddressByLaAndLo(longitude, latitude);
            JSONObject result = JSON.parseObject(resultStr, JSONObject.class);
//            String string = result.getJSONObject("result").getString("formatted_address");
            JSONObject addressComponent = JSON.parseObject(result.getJSONObject("result").getString("addressComponent"),
                    JSONObject.class);
            rKey = addressComponent.getString("city") + addressComponent.getString("district");
        } else {
            rKey = cunDTO.getXian();
        }

        //缓存过期时间 5小时
        Long expireTime = (long) (5 * 60 * 60);
        //获取当前实时天气
        List<DailyWeather> nowWeathers;
        String nowWeatherJson = RedisUtils.get(RedisKeyConstant.DAY_WEATHER + rKey);
        if (StringUtils.isNotEmpty(nowWeatherJson)) {
            nowWeathers = JSON.parseArray(nowWeatherJson, DailyWeather.class);
        } else {
            nowWeathers = weatherService.getWeather(latitude + ":" + longitude, 0, 5);
            RedisUtils.set(RedisKeyConstant.DAY_WEATHER + rKey, JSON.toJSONString(nowWeathers), expireTime);
        }
        return DataReturn.ok(nowWeathers);
    }

    @GetMapping(value = "/queryByFireId", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询火情天气情况", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<FireWeatherInfo> queryByFireId(@RequestParam String fireId) {
        return DataReturn.ok(fireWeatherInfoService.queryByFireId(Long.parseLong(fireId)));
    }

    @GetMapping(value = "/getSpreadInfo", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取蔓延数据")
    public DataReturn getSpreadInfo(@RequestParam("location") String location) {
        NowWeather nowWeather = weatherService.getNowWeather(location);
        Double spreadRate = weatherService.getSpreadRate(nowWeather);
        if (nowWeather != null) {
            SpreadDTO spreadDTO = new SpreadDTO(nowWeather, spreadRate);
            return DataReturn.ok(spreadDTO);
        }
        return DataReturn.ok();
    }

}
